import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout } from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'
import { NO_RESET_WHITE_LIST } from '@/constants'

const { t } = useI18n()

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard/analysis',
    name: 'Root',
    meta: {
      hidden: true
    }
  },
  {
    path: '/redirect',
    component: Layout,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'RedirectA',
        component: () => import('@/views/Redirect/Redirect.vue'),
        meta: {}
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: t('router.login'),
      noTagsView: true
    }
  },
  {
    path: '/personal',
    component: Layout,
    redirect: '/personal/personal-center',
    name: 'Personal',
    meta: {
      title: t('router.personal'),
      hidden: true,
      canTo: true
    },
    children: [
      {
        path: 'personal-center',
        component: () => import('@/views/Personal/PersonalCenter/PersonalCenter.vue'),
        name: 'PersonalCenter',
        meta: {
          title: t('router.personalCenter'),
          hidden: true,
          canTo: true
        }
      }
    ]
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  }
]

export const asyncRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/dashboard',
    component: Layout,
    redirect: '/dashboard/analysis',
    name: 'Dashboard',
    meta: {
      title: t('广告平台管理'),
      icon: 'vi-ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'analysis',
        component: () => import('@/views/Dashboard/Analysis.vue'),
        name: 'Analysis',
        meta: {
          title: t('广告时段统计'),
          noCache: true
        }
      }
      // {
      //   path: 'workplace',
      //   component: () => import('@/views/Dashboard/ApplyDay.vue'),
      //   name: 'ApplyDay',
      //   meta: {
      //     title: t('广告天数统计'),
      //     noCache: true
      //   }
      // }
    ]
  },
  {
    path: '/user-manage',
    component: Layout,
    redirect: '/user-manage/all-users',
    name: 'UserManage',
    meta: {
      title: t('用户管理'),
      icon: 'ep:user-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'all-users',
        component: () => import('@/views/UsersManage/AllUsers/index.vue'),
        name: 'AllUsers',
        meta: {
          title: t('所有用户'),
          noCache: true,
          affix: false
        }
      },
      // {
      //   path: 'add-users',
      //   component: () => import('@/views/UserManage/AddUsers/index.vue'),
      //   name: 'AddUsers',
      //   meta: {
      //     title: t('新增用户'),
      //     noCache: true
      //   }
      // },
      {
        path: 'remain',
        component: () => import('@/views/UsersManage/Remain/index.vue'),
        name: 'Remain',
        meta: {
          title: t('留存情况'),
          noCache: true
        }
      },
      {
        path: 'advertising-total',
        component: () => import('@/views/UsersManage/AdvertisingTotal/index.vue'),
        name: 'AdvertisingTotal',
        meta: {
          title: t('广告条数筛选'),
          noCache: true
        }
      }
    ]
  },
  {
    path: '/earnings-manage',
    component: Layout,
    redirect: '/earnings-manage/real-earnings',
    name: 'EarningsManage',
    meta: {
      title: t('收益管理'),
      icon: 'ep:histogram',
      alwaysShow: true
    },
    children: [
      {
        path: 'real-earnings',
        component: () => import('@/views/EarningsManage/RealEarnings/index.vue'),
        name: 'RealEarnings',
        meta: {
          title: t('实际收益'),
          noCache: true
        }
      }
    ]
  },
  {
    path: '/partner-manage',
    component: Layout,
    redirect: '/partner-manage/realtimeStatistics',
    name: 'PartnerManage',
    meta: {
      title: t('合伙人管理'),
      icon: 'ep:share',
      alwaysShow: false
    },
    children: [
      {
        path: 'realtimeStatistics',
        component: () => import('@/views/PartnerManage/RealtimeStatistics/index.vue'),
        name: 'RealtimeStatistics',
        meta: {
          title: t('新增实时统计'),
          noCache: true
        }
      },
      // {
      //   path: 'agent',
      //   component: () => import('@/views/PartnerManage/Agent/index.vue'),
      //   name: 'Agent',
      //   meta: {
      //     title: t('代理商管理'),
      //     noCache: true
      //   }
      // },
      {
        path: 'addPartner',
        component: () => import('@/views/PartnerManage/AddPartner/index.vue'),
        name: 'AddPartner',
        meta: {
          title: t('合伙人管理'),
          noCache: true
        }
      }
    ]
  },
  {
    path: '/permission-setting',
    component: Layout,
    redirect: '/permission-setting/prorate',
    name: 'PermissionSetting',
    meta: {
      title: t('权限与风控'),
      icon: 'ep:unlock',
      alwaysShow: true
    },
    children: [
      {
        path: 'prorate',
        component: () => import('@/views/PermissionSetting/Permission/index.vue'),
        name: 'Prorate',
        meta: {
          title: t('比例分配'),
          noCache: true
        }
      },
      {
        path: 'ipRestriction',
        component: () => import('@/views/PermissionSetting/IpRestriction/index.vue'),
        name: 'IpRestriction',
        meta: {
          title: t('IP限制'),
          noCache: true
        }
      },
      {
        path: 'withdraw',
        component: () => import('@/views/PermissionSetting/Withdraw/index.vue'),
        name: 'Withdraw',
        meta: {
          title: t('提现审批'),
          noCache: true
        }
      },
      {
        path: 'qrCode-manage',
        component: () => import('@/views/PermissionSetting/QRcodeManage/index.vue'),
        name: 'QRcodeManage',
        meta: {
          title: t('二维码管理'),
          noCache: true
        }
      },
      {
        path: 'video-manage',
        component: () => import('@/views/PermissionSetting/VideoManage/index.vue'),
        name: 'VideoManage',
        meta: {
          title: t('视频管理'),
          noCache: true
        }
      },
      {
        path: 'equities-manage',
        component: () => import('@/views/PermissionSetting/EquitiesManage/index.vue'),
        name: 'EquitiesManage',
        meta: {
          title: t('权益中心管理'),
          noCache: true
        }
      }
    ]
  }
  // {
  //   path: '/system-settings',
  //   component: Layout,
  //   redirect: '/system-settings/complaints',
  //   name: 'SystemSettings',
  //   meta: {
  //     title: '系统设置',
  //     icon: 'icon-park-outline:setting-computer',
  //     alwaysShow: true
  //   },
  //   children: [
  //     {
  //       path: 'menu-manage',
  //       component: () => import('@/views/SystemSettings/MenuManage/index.vue'),
  //       name: 'MenuManage',
  //       meta: {
  //         title: '菜单管理'
  //       }
  //     },
  //     {
  //       path: 'role-manage',
  //       component: () => import('@/views/SystemSettings/RoleManage/index.vue'),
  //       name: 'RoleManage',
  //       meta: {
  //         title: '角色管理'
  //       }
  //     },
  //     {
  //       path: 'button-manage',
  //       component: () => import('@/views/SystemSettings/ButtonManage/index.vue'),
  //       name: 'ButtonManage',
  //       meta: {
  //         title: '按钮管理'
  //       }
  //     }
  //   ]
  // }
]

const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !NO_RESET_WHITE_LIST.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
